package com.rchat.pocmini.common.mvp;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;

import com.aso.common.dialog.LoadingDialog;
import com.aso.common.http.utils.NetErrCode;
import com.aso.common.utils.TUtil;
import com.aso.common.utils.ToastUtils;
import com.itsdf07.alog.ALog;
import com.rchat.pocmini.R;
import com.rchat.pocmini.bean2eventbus.BluetoothBean;
import com.rchat.pocmini.bluetooth.BTService;
import com.rchat.pocmini.common.utils.ActivityStackManager;
import com.rchat.pocmini.service.PocService;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindString;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * ButterKnife.bind(this)：快捷插件库
 * Created by itsdf07 on 2017/7/25.
 */

public abstract class BaseMvpActivity<P extends BasePresenter, M extends BaseModel> extends AppCompatActivity implements
        BaseView{
    public P mPresenter;
    public M mModel;

    public Activity mActivity;

    public LoadingDialog mLoadingDialog;

    protected Unbinder mUnbinder;

    @BindString(R.string.tip_net_error)
    String mStringTip_error;
    @BindString(R.string.tip_loading)
    String mTipLoading;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ALog.d("onCreate :savedInstanceState = %s", savedInstanceState);
        ALog.d("onCreate : this = %s", this);
        super.onCreate(savedInstanceState);
        doBeforeSetContentView();
        setContentView(getLayoutId());
        mUnbinder = ButterKnife.bind(this);
        mActivity = this;
        mPresenter = TUtil.getT(this, 0);
        mModel = TUtil.getT(this, 1);
        if (mPresenter != null) {
            mPresenter.mActivity = mActivity;
        }
        this.initPresenter();
        this.initView();
        this.doAfterInitView();
    }

    @Override
    protected void onStart() {
        ALog.d("onStart : this = %s", this);
        super.onStart();
    }

    @Override
    protected void onRestart() {
        ALog.d("onRestart : this = %s", this);
        super.onRestart();
    }

    @Override
    protected void onResume() {
        ALog.d("onResume : this = %s", this);
        super.onResume();
        if (mPresenter != null) {
            mPresenter.onPResume();
        }
        //TODO 友盟统计之类的
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        ALog.d("onPause : this = %s", this);
        super.onPause();
        if (mPresenter != null) {
            mPresenter.onPPause();
        }
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onStop() {
        ALog.d("onStop : this = %s", this);
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        ALog.d("onDestroy : this = %s", this);
        ActivityStackManager.getStackManagerUtils().finishActivity(this);
        if (mPresenter != null) {
            mPresenter.onPDestroy();
        }
        mUnbinder.unbind();
        super.onDestroy();
    }
    /*************************be related to custom methods ********************************/
    /**
     * 设置layout前配置
     */
    private void doBeforeSetContentView() {
        // 把activity放到application栈中管理
        ActivityStackManager.getStackManagerUtils().addActivity(this);
    }

    /*********************子类实现*****************************/

    /**
     * 简单页面无需mvp就不用管此方法即可,完美兼容各种实际场景的变通
     */
    public abstract void initPresenter();

    /**
     * 获取布局文件
     *
     * @return
     */
    public abstract int getLayoutId();

    /**
     * 初始化view
     */
    public abstract void initView();

    public abstract void doAfterInitView();

    /*************************be related to Tip********************************/
    /**
     * 短暂显示Toast提示
     *
     * @param tip
     */
    @Override
    public void showShortToast(String tip) {
        ToastUtils.showToast(this, tip);
    }

    /**
     * 短暂显示Toast提示
     *
     * @param tipId
     */
    @Override
    public void showShortToast(int tipId) {
        showShortToast(getString(tipId));
    }

    /**
     * 显示网络异常toast
     **/
    @Override
    public void showNetError() {
        showNetError(mStringTip_error);
    }

    /**
     * 显示网络异常toast
     *
     * @param errorTip
     */
    @Override
    public void showNetError(String errorTip) {
        showShortToast(errorTip);
    }

    /**
     * 显示网络异常toast
     *
     * @param errCode
     * @param errorTip
     */
    @Override
    public void showNetError(String errCode, String errorTip) {
        if (NetErrCode.ERR_FAILURE.equals(errCode)) {
            showNetError();
        } else {
            showShortToast(errorTip);
        }
    }

    /**
     * 显示网络异常toast
     *
     * @param errorTip
     */
    @Override
    public void showNetError(int errorTip) {
        showShortToast(errorTip);
    }

    @Override
    public void showLoading() {
        showLoading(mTipLoading);
    }

    @Override
    public void showLoading(String content) {
        if (null == mLoadingDialog) {
            if (TextUtils.isEmpty(content)) {
                content = mTipLoading;
            }
            mLoadingDialog = new LoadingDialog(this, content);//getString(R.string.tip_loading)
        }
        mLoadingDialog.show();
    }

    @Override
    public void dismissLoading() {
        if (null != mLoadingDialog) {
            mLoadingDialog.dismiss();
        }
    }

    /**
     * 使用系统返回指令
     */
    public void doGoBack() {
        KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
//        ((Activity) mContext).dispatchKeyEvent(event);
        dispatchKeyEvent(event);
        event = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK);
//        ((Activity) mContext).dispatchKeyEvent(event);
        dispatchKeyEvent(event);
    }

    private boolean isDown = false;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK) {
            if (!isDown) {
                isDown = true;
                BluetoothBean bluetoothBean = new BluetoothBean();
                bluetoothBean.setCurrentEvent(BluetoothBean.EVENT_PTT_DOWN);
                bluetoothBean.setTarget(this.getClass(), BTService.class, "onBluetoothEventBus");
                EventBus.getDefault().post(bluetoothBean);
            } else {
                isDown = false;
                BluetoothBean bluetoothBean = new BluetoothBean();
                bluetoothBean.setCurrentEvent(BluetoothBean.EVENT_PTT_UP);
                bluetoothBean.setTarget(this.getClass(), BTService.class, "onBluetoothEventBus");
                EventBus.getDefault().post(bluetoothBean);
            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    /************************* 广播、handler、接口 之类********************************/

}
